package cn.concurrence.cas;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;

/**
 * @author summit
 * @since 2022/4/21 11:11
 */
public class ArrayListTest {

	/**
	 *     public boolean add(E e) {
	 *         final ReentrantLock lock = this.lock;
	 *         lock.lock();
	 *         try {
	 *             Object[] elements = getArray();
	 *             int len = elements.length;
	 *             Object[] newElements = Arrays.copyOf(elements, len + 1);
	 *             newElements[len] = e;
	 *             setArray(newElements);
	 *             return true;
	 *         } finally {
	 *             lock.unlock();
	 *         }
	 *     }
	 */
	public static void main(String[] args) throws InterruptedException {
		//arraylistSafe();

		List<String> list = new CopyOnWriteArrayList<>();
	}

	private static void arraylistSafe() throws InterruptedException {
		List<String> list = new ArrayList<>();

		for (int i = 0; i < 100; i++) {
			new Thread(()->{
				list.add(Math.random() + "");
			}).start();
		}

		TimeUnit.SECONDS.sleep(10);
	}
}
